package com.qmerak.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 每个form是一个对象。主要解决同一个Session保存多个Form的file上传进度
 * 
 * @author hexin
 * 
 */
public class FromProgressModel implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2584883440543478833L;

	private static final Logger LOG = LoggerFactory.getLogger(FromProgressModel.class);

	private Map<Integer, Map<String, String>> map;

	public FromProgressModel() {
		map = new HashMap<Integer, Map<String, String>>();
	}

	public void addItem(int itemId, String fieldName) {
		LOG.debug("  addItem itemId >> " + itemId + "  fieldName >> " + fieldName);

		Map<String, String> item = map.get(itemId);
		if (item == null) {
			item = new HashMap<String, String>();
			item.put("fieldName", fieldName);
		} else {
			item.put("fieldName", fieldName);
		}
		map.put(itemId, item);
	}

	public Map<String, String> getItemMap(int itemId) {
		return map.get(itemId);
	}

	public void updateProgress(int itemId, String progress) {
		LOG.debug(" add updateProgress itemId >> " + itemId + "  progress >> " + progress);
		Map<String, String> item = map.get(itemId);
		if (item != null) {
			item.put("progress", progress);
		} else {
			item = new HashMap<String, String>();
			item.put("progress", progress);
		}
		map.put(itemId, item);
	}

	public String getProgress(String fieldName) {
		String progress = "";
		for (Map.Entry<Integer, Map<String, String>> entry : map.entrySet()) {
			Map<String, String> progressMap = entry.getValue();
			if (fieldName.equals(progressMap.get("fieldName"))) {
				progress = progressMap.get("progress");
			}
		}
		return progress;
	}

	public Map<Integer, Map<String, String>> getFromMap() {
		return map;
	}
}
